<?php
include_once "model/service/AccountManager.php";

include_once "controller/action/Action.php";

class AccountAction extends Action {
	public function __construct() {
		$this->manager = new AccountManager();
	}

	public function execute() {
		echo "AccountAction::execute();";
	}

	public function index() {
		$inAccountList = $this->manager->query(array ("type" => "in"));
		/*$inAccountList = $this->manager->findAll(array (
			"start" => 0,
			"size" => 2
			));*/
		$outAccountList = $this->manager->query(array ("type" => "out"));
		return array(
			"in" => $inAccountList,
			"out" => $outAccountList
		);
	}

	public function indexByDate($date) {
		$accountList = $this->manager->query(array ("date" => $date));
		
		$jsonResult = array();
		
		foreach ($accountList as $account) {
			$jsonResult[] = $account->toJSON();
		}
		echo json_encode(array ( "result" => $jsonResult));
		return $accountList;
	}
	
	public function indexByDay() {
		$inAccountList = $this->manager->query(array ("type" => "in"));

		$result = array();

		foreach ($inAccountList as $account) {
			$date = $account->getDate();
			if (!$result[$date]) {
				$result[$date] = array();
			}

			$result[$date][] = $account;
		}
		return $result;
	}

	public function indexByMonth() {
		$accountList = $this->manager->findAll(array(
			"orderby" => "date"
		));

		$result = array();

		foreach ($accountList as $account) {
			$date = substr($account->getDate(), 0, 7);
			$type = $account->getType();
			if (!$result[$date]) {
				$result[$date] = array(
						"in" => array(),
						"out" => array()
				);
			}
			$result[$date][$type][] = $account;
		}
		return $result;
	}

	public function indexByYear() {
		$inAccountList = $this->manager->query(array ("type" => "in"));

		$result = array();

		foreach ($inAccountList as $account) {
			$date = substr($account->getDate(), 0, 4);

			if (!$result[$date]) {
				$result[$date] = array();
			}

			$result[$date][] = $account;
		}
		return $result;
	}

	public function show() {
		$id = $_GET["id"];

		if ($id) {
			$account = $this->manager->findById($id);

			return $account;
		}
	}
	public function save() {
		$type = $_GET["type"];

		$category = $_POST["category"];
		$date = $_POST["date"];
		$amount = $_POST["amount"];
		$tag = $_POST["tag"];
		$description = $_POST["description"];
		$detail = $_POST["detail"];

		$account = new Account($type, $category, $date, $amount, $tag, $description, $detail);

		$this->manager->save($account);

		echo json_encode($account->toJSON());
	}


	public function remove() {
		$id = $_POST["id"];

		if ($id) {
			$result = $this->manager->removeById($id);

			$jsonResult = array (
				"id" => $id,
				"done" => $result
			);

			echo json_encode($jsonResult);
		}
	}

	public function stats() {
		$sqlIn = "SELECT COUNT(ACCOUNT_AMOUNT) AS count, SUM(ACCOUNT_AMOUNT) AS amount FROM MONEY_ACCOUNT WHERE ACCOUNT_TYPE='in';";
		$statsIn = $this->manager->execute($sqlIn);

		$sqlOut = "SELECT COUNT(ACCOUNT_AMOUNT) AS count, SUM(ACCOUNT_AMOUNT) AS amount FROM MONEY_ACCOUNT WHERE ACCOUNT_TYPE='out';";
		$statsOut = $this->manager->execute($sqlOut);

		$statsIn[0]["category"] = "Income";
		$statsOut[0]["category"] = "Expenditure";

		$accountStats = array(
		$statsIn[0],
		$statsOut[0],
		array(
				"category" => "Total",
				"count" => "1",
				"amount" => $statsIn[0]["amount"] - $statsOut[0]["amount"]
		)
		);

		return $accountStats;
	}

	public function statisticByDay() {
		$recordList = $this->manager->statisticByDay();

		$result = array();
		foreach($recordList as $record) {
			$month = substr($record["date"], 0, 7);
			if (!$result[$month]) {
				$result[$month] = array();
			}
			
			$day = $record["date"];
			if (!$result[$month][$day]) {
				$result[$month][$day] = array(
					"date" => $day,
					"in" => 0,
					"out" => 0,
					"total" => 0
				);
			}
			
			if ($record["type"] == "in") {
				$result[$month][$day]["in"] += $record["amount"];
				$result[$month][$day]["total"] += $record["amount"];
			} else {
				$result[$month][$day]["out"] += $record["amount"];
				$result[$month][$day]["total"] -= $record["amount"];
			}
		}
		
		return $result;
	}
	
	public function selectCategory() {
		$inCategory = $this->manager->selectCategory("in");
		$outCategory = $this->manager->selectCategory("out");

		$category = array(
			"in" => $inCategory,
			"out" => $outCategory
		);

		return $category;
	}
}
?>